#include "chart.h"

Chart::Chart(QWidget *parent):QWidget(parent),lastTextItem1(nullptr),lastTextItem2(nullptr)
{
    chart1 = new QtCharts::QChart();
    series1 = new QtCharts::QLineSeries();
    QPen pen1;
    pen1.setColor(Qt::blue);  // 设定颜色为蓝色
    pen1.setWidth(2);         // 设定线宽
    series1->setPen(pen1);
    chart1->addSeries(series1);

    axisX1 = new QtCharts::QValueAxis();
    axisY1 = new QtCharts::QValueAxis();

    axisX1->setRange(0, 100);
    axisY1->setRange(0, 500);  // 设置传感器数值范围

    chart1->setAxisX(axisX1, series1);
    chart1->setAxisY(axisY1, series1);

    chart1->setTitle("Chart1:Sensor");

    QtCharts::QChartView *chartView1 = new QtCharts::QChartView(chart1);
    chartView1->setRenderHint(QPainter::Antialiasing);

    chart2 = new QtCharts::QChart();
    series2 = new QtCharts::QLineSeries();
    QPen pen2;
    pen2.setColor(Qt::red);   // 设定颜色为红色
    pen2.setWidth(2);         // 设定线宽
    series2->setPen(pen2);
    chart2->addSeries(series2);

    axisX2 = new QtCharts::QValueAxis();
    axisY2 = new QtCharts::QValueAxis();

    axisX2->setRange(0, 100);
    axisY2->setRange(0, 1000);  // 设置电机数值范围

    chart2->setAxisX(axisX2, series2);
    chart2->setAxisY(axisY2, series2);

    chart2->setTitle("Chart2:Motor");

    QtCharts::QChartView *chartView2 = new QtCharts::QChartView(chart2);
    chartView1->setRenderHint(QPainter::Antialiasing);

    QVBoxLayout *layout=new QVBoxLayout(this);
    layout->addWidget(chartView1);
    layout->addWidget(chartView2);
    setLayout(layout);
}

Chart::~Chart()
{
    delete chart1;
    delete series1;
    delete axisX1;
    delete axisY1;

    delete chart2;
    delete series2;
    delete axisX2;
    delete axisY2;

    delete lastTextItem1;
    delete lastTextItem2;
}

void Chart::updateChart()
{
    series1->append(x++,*sensor);
    series2->append(x1++,*position);
    if (series1->count() > 100) {
        series1->remove(0);
    }

    if (series2->count() > 100) {
        series2->remove(0);
    }
    axisX1->setRange(x-100,x);
    axisX2->setRange(x1-100,x1);

    if (!series1->points().isEmpty()) {
        QPointF lastPoint1 = series1->points().last();
        if(lastTextItem1)
        {
            chart1->scene()->removeItem(lastTextItem1);
            delete lastTextItem1;
        }
        lastTextItem1=new QGraphicsSimpleTextItem(QString::number(lastPoint1.y()));
        lastTextItem1->setPos(chart1->mapToPosition(lastPoint1, series1).x(), chart1->mapToPosition(lastPoint1, series1).y() - 15);
        chart1->scene()->addItem(lastTextItem1);
    }

    // 显示 series2 的最后一个点的 Y 值
    if (!series2->points().isEmpty()) {
        QPointF lastPoint2 = series2->points().last();
        if(lastTextItem2)
        {
            chart2->scene()->removeItem(lastTextItem2);
            delete lastTextItem2;
        }
        lastTextItem2=new QGraphicsSimpleTextItem(QString::number(lastPoint2.y()));
        lastTextItem2->setPos(chart2->mapToPosition(lastPoint2, series2).x(), chart2->mapToPosition(lastPoint2, series2).y() - 15);
        chart2->scene()->addItem(lastTextItem2);
    }

}
